package com.example.software.controller;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

// import org.apache.shiro.authz.annotation.RequiresPermissions;
import com.example.software.dto.DepartmentDTO;
import com.example.software.entity.Result;
import com.example.software.vo.DepartmentVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import com.example.software.entity.DepartmentEntity;
import com.example.software.service.DepartmentService;
import com.example.software.utils.PageUtils;
import com.example.software.utils.R;



/**
 * 科室分类树状表
 *
 * @author wxy
 * @email wxy9496099@163.com
 * @date 2024-10-28 10:54:38
 */
@RestController
@Slf4j
@Api(tags = "科室相关接口")
@RequestMapping("software/department")
public class DepartmentController {
    @Autowired
    private DepartmentService departmentService;



    /**
     * 列表
     */
    @GetMapping("/list")
    @ApiOperation(value = "科室列表查询")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "page", value = "页数", required = true),
            @ApiImplicitParam(name = "size", value = "每页数量",dataType = "int"),
            @ApiImplicitParam(name = "name", value = "模糊查询，如果查全部可以不传或者传0")
    })
   // @RequiresPermissions("software:department:list")
    public Result<PageUtils> list(@RequestParam String page,
                                  @RequestParam String size,
                                  @RequestParam(required = false) String name){

        Map<String, Object> params = new HashMap<>();
        params.put("page",page);
        params.put("limit",size);
        if (name != null && !name.equals("0")) {
            params.put("name",name);
        }


        PageUtils page2 = departmentService.queryPage(params);

        return Result.success(page2);
    }


    /**
     * 信息
     */
    @GetMapping("/info/{id}")
    @ApiOperation(value = "根据id查询科室信息")
    // @RequiresPermissions("software:department:info")
    public Result info(@PathVariable("id") Long id){

		DepartmentEntity department = departmentService.getById(id);
        DepartmentVO departmentVO = new DepartmentVO();

        //属性拷贝
        BeanUtils.copyProperties(department, departmentVO);


        return Result.success(departmentVO);
    }

    /**
     * 保存
     */
    @PostMapping
    @ApiOperation(value = "新增科室信息")
    // @RequiresPermissions("software:department:save")
    public R save(@RequestBody DepartmentDTO departmentDTO){

        DepartmentEntity department = new DepartmentEntity();
        BeanUtils.copyProperties(departmentDTO, department);

		departmentService.save(department);

        return R.ok();
    }

    /**
     * 修改
     */
    @PutMapping
    @ApiOperation(value = "修改科室信息")
    // @RequiresPermissions("software:department:update")
    public R update(@RequestBody DepartmentDTO departmentDTO){

        DepartmentEntity department = new DepartmentEntity();
        BeanUtils.copyProperties(departmentDTO, department);

		departmentService.updateById(department);

        return R.ok();
    }

    /**
     * 删除
     */
    @DeleteMapping
    @ApiOperation(value = "根据id批量删除科室信息")
    // @RequiresPermissions("software:department:delete")
    public R delete(@RequestBody Long[] ids){
		departmentService.removeByIds(Arrays.asList(ids));

        return R.ok();
    }

}
